package engine.newUi
{
	import engine.GameData;
	import engine.data.ModelLocator;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.scene.SceneManager;
	import engine.ui.SystemConfigureUI;
	import engine.ui.lite.BaseUI;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.net.SharedObject;
	import flash.utils.Timer;
	
	import newx.com.config.StaticIni;
	import newx.com.control.button.NxButton;
	
	/**
	 * 雪人逻辑 
	 * @author gf
	 * 
	 */	
	
	public class SnowBossExitUI extends BaseUI
	{
		public var displayMc				:MovieClip;
		public var exitBtn					:NxButton;
		public var showHideBtn				:NxButton;
		
		private static var self			:SnowBossExitUI;
		private var model					:ModelLocator;
		
		/**刷新计时器*/		
		private 	var 	cdTime			:Timer 	= new Timer(1000);
		
		/**副本所在人数*/
		private var peopleNum				:int	=	-1;
		/**离开副本时间*/
		private var exitTime				:int 	=	-1;
		
		private var isHide					:Boolean = false;
		
		public function SnowBossExitUI(popupUI:Boolean=true)
		{
			super(popupUI);
		}
		
		public static function getInstance():SnowBossExitUI
		{
			if(self == null)
			{
				self 		= 	new SnowBossExitUI();
			}
			return self;
		}
		
		override public function Init():void
		{
			model						=	ModelLocator.getInstance();
			model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_SNOWBALL_FIGHT_LEFT_TIME),onShow);
		}
		
		override protected function BeforeShow():void
		{
			addEvents();
			var userdata:SharedObject	=	SharedObject.getLocal("userdata");
			isHide	=	userdata.data.playGroupIndex==0?false:true;
			showHideBtn.label = isHide ? StaticIni.getSwfLangStr("UIStr_1000731") : StaticIni.getSwfLangStr("UIStr_1001635");
		}
		
		override protected function AfterHide():void
		{
			removeEvents();
		}
		
		private function addEvents():void
		{
			exitBtn.addEventListener(MouseEvent.CLICK,onExit);
			showHideBtn.addEventListener(MouseEvent.CLICK,onShowHide);
			cdTime.addEventListener(TimerEvent.TIMER,onTimeCount);
		}
		
		private function removeEvents():void
		{
			exitBtn.removeEventListener(MouseEvent.CLICK,onExit);
			showHideBtn.removeEventListener(MouseEvent.CLICK,onShowHide);
			cdTime.removeEventListener(TimerEvent.TIMER,onTimeCount);
		}
		
		private function onExit(e:MouseEvent):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.SERVER_CUSTOMMSG_SEND_SNOWBALL_FIGHT_LEAVE_SCENE);
		}
		
		private function onShowHide(e:MouseEvent):void
		{
			var userdata:SharedObject	=	SharedObject.getLocal("userdata");
			isHide	=	!isHide;
			showHideBtn.label = isHide ? StaticIni.getSwfLangStr("UIStr_1000731") : StaticIni.getSwfLangStr("UIStr_1001635");
			SceneManager.getInstance().norSceneView.scenePlayerLogic.changePlayerVisible( !isHide );
			RightTopIndexLogic.getInstance().changeDisplayShow(isHide);
			SystemConfigureUI.getInstance().playGroup.selectedIndex	=	isHide?1:0;
			userdata.data.playGroupIndex	=	isHide?1:0;
		}
		
		private function onTimeCount(e:TimerEvent):void
		{
			exitTime--;
			if(exitTime < 0)
			{
				cdTime.stop();
				return;
			}
			displayMc["txt2"].htmlText					=	peopleNum;
			var timeArr				:Array					=	getTime(exitTime);
			displayMc["txt4"].htmlText					=	timeArr[0] + ":" + timeArr[1] + ":" + timeArr[2];
		}
		
		private function onShow(e:CustomMsgEvent):void
		{
			var info		:Array				=	e.pList;
			peopleNum							=	int(info[0]);
			exitTime							=	int(info[1]);
			
			displayMc["txt3"].htmlText				=	StaticIni.getSwfLangStr("UIString_1000197");//剩余时间
			
			if(exitTime > 0)
			{
				cdTime.reset();
				cdTime.start();
				displayMc["txt1"].visible=displayMc["txt2"].visible=displayMc["txt3"].visible = displayMc["txt4"].visible = true;
			}
			else
			{
				displayMc["txt1"].visible=displayMc["txt2"].visible=displayMc["txt3"].visible = displayMc["txt4"].visible = false;
			}
		}
		
		private function getTime(timeSec:int):Array
		{
			timeSec	=	timeSec<0	?	0	:	timeSec;
			var d:int	=	int(	timeSec/(60*60*24)				);
			var h:int	=	int(	timeSec%(60*60*24)/(60*60)		);
			var m:int	=	int(	timeSec%(60*60)	/60				);
			var s:int	=	int(	timeSec%60						);
			
			var hour:String  	=	h<10 ? "0"+h : ""+h;
			var minute:String  	=	m<10 ? "0"+m : ""+m;
			var secend:String  	=	s<10 ? "0"+s : ""+s;
			
			return [hour,minute,secend];
		}
		
	}
}